home *** CD-ROM | disk | FTP | other *** search
Wrap
Path: unsw.edu.au!usenet From: jeroen@fatboy.gas.unsw.edu.au (Jeroen Dijkmeijer) Newsgroups: comp.lang.c++ Subject: template problem Date: 28 Mar 1996 05:51:59 GMT Organization: Centre for Remote Sensing and GIS, UNSW. Message-ID: <4jd9dv$27g@mirv.unsw.edu.au> Reply-To: jeroen@fatboy.gas.unsw.edu.au NNTP-Posting-Host: wideglide.geog.unsw.edu.au Keywords: template, friend declaration. Problem with templates and friend declaration G'day I haven't much of C++ experience. But I'm studying hard to learn. At this moment I do have a problem with templates. The example is taken from Deitel & Deitel C++ how to program ch 15 pg 697 The files are listed below. // file listnode.h // List template definition #ifndef LISTNODE_H #define LISTNODE_H template<class DATA_TYPE> class ListNode { friend class List<DATA_TYPE>; public: ListNode(const DATA_TYPE &); //constructor DATA_TYPE getData() const; private: DATA_TYPE data; // data ListNode *nextPtr; }; template<class DATA_TYPE> ListNode<DATA_TYPE>::ListNode(const DATA_TYPE &info) { data = info; nextPtr = NULL; } template<class DATA_TYPE> DATA_TYPE ListNode<DATA_TYPE>::getData() const { return data; } #endif // file list.h // Template definition of a List class. #ifndef LIST_H #define LIST_H #include <iostream.h> #include <assert.h> #include "listnode.h" template<class DATA_TYPE> class List { public: List(); //constructor ~List(); //destructor void insertAtFront(const DATA_TYPE &); int removeFromFront(DATA_TYPE &); int isEmpty() const; int member(const DATA_TYPE &) const; void print() const; private: ListNode <DATA_TYPE> *firstPtr; ListNode <DATA_TYPE> *lastPtr; ListNode <DATA_TYPE> *getNewNode(const DATA_TYPE &); }; //---------------------------------------- member definitions for List template<class DATA_TYPE> List<DATA_TYPE>::List() {firstPtr = lastPtr = NULL; } // Destructor template<class DATA_TYPE> List<DATA_TYPE>::~List() { for(int i=0;i<10;i++); if(!isEmpty()) { cout<<"Destroying nodes ... "<<endl; ListNode<DATA_TYPE> *currentPtr = firstPtr, *tempPtr; while (currentPtr != NULL) { tempPtr = currentPtr; cout <<"deleting "<< tempPtr->data << endl; currentPtr = currentPtr -> nextPtr; delete tempPtr; } } cout << "All nodes destroyed" << endl <<endl; } // Insert a node at the front of the list template<class DATA_TYPE> void List<DATA_TYPE>::insertAtFront(const DATA_TYPE &value) { ListNode<DATA_TYPE> *newPtr = getNewNode(value); if (isEmpty()) firstPtr = lastPtr = newPtr; else { newPtr->nextPtr = firstPtr; firstPtr = newPtr; } } template<class DATA_TYPE> int List<DATA_TYPE>::removeFromFront(DATA_TYPE &info) { if (isEmpty()) return 0; else { ListNode<DATA_TYPE> *tempPtr = firstPtr; if (firstPtr == lastPtr) firstPtr = lastPtr = 0; else firstPtr = firstPtr->nextPtr; info = tempPtr->data; delete tempPtr; return 1; } } // RemoveFromFront(DATA_TYPE &info) template<class DATA_TYPE> int List<DATA_TYPE>::isEmpty() const {return firstPtr == NULL; } template<class DATA_TYPE> ListNode<DATA_TYPE> *List<DATA_TYPE>::getNewNode(const DATA_TYPE &value) { ListNode<DATA_TYPE> *ptr = new ListNode<DATA_TYPE>(value); assert(ptr != 0); return ptr; } // getNewNode(const DATA_TYPE &data) // Display the contents of the List template<class DATA_TYPE> void List<DATA_TYPE>::print() const { if(isEmpty()) { cout << "The list is empty" << endl <<endl; return; } ListNode<DATA_TYPE> *currentPtr = firstPtr; cout << "The List is: "; while (currentPtr != 0) { cout <<currentPtr->data << ' '; currentPtr = currentPtr->nextPtr; } cout << endl << endl; } template<class DATA_TYPE> int List<DATA_TYPE>::member(const DATA_TYPE &info) const { if(isEmpty()) return 0; else { ListNode<DATA_TYPE> *currentPtr = firstPtr; while(currentPtr != NULL) { if(currentPtr->data == info) return 1; currentPtr = currentPtr->nextPtr; } return 0; } } #endif // file list_driver.cpp #include <iostream.h> #include "list.h" #include "pair.h" void testIntegerList(); void testPairList(); main() { testIntegerList(); testPairList(); return 0; } void testIntegerList() { cout << "testing a list of integer values" << endl; List<int> integerList; int choice, value; do { cout << "? "; cin >> choice; switch (choice) { case 1: cout << "Enter an integer: "; cin >> value; integerList.insertAtFront(value); integerList.print(); break; case 2: cout << "Deleting a node"<<endl; integerList.removeFromFront(value); cout<<"The value was"<<value<<endl; break; case 3: cin>>value; if(integerList.member(value)) cout<<value<<"is element of list"<<endl; else cout<<value<<"is NOT a member of list"<<endl; break; } } while (choice != 4); cout << "End test of integerList" <<endl; } void testPairList() { cout << "testing a list of pair values" << endl; List<Pair> pairList; int choice; Pair value; do { cout << "? "; cin >> choice; switch (choice) { case 1: cout << "Enter a pair: "; cin >> value; pairList.insertAtFront(value); pairList.print(); break; case 2: cout << "Deleting a node"<<endl; pairList.removeFromFront(value); cout<<"The value was"<<value<<endl; break; case 3: cin>>value; if(pairList.member(value)) cout<<value<<"is element of list"<<endl; else cout<<value<<"is NOT a member of list"<<endl; break; } } while (choice != 4); cout << "End test of pairList" <<endl; } void instructions() { cout<<"Your choices are:"<<endl; cout<<"1: Add an value to the list"<<endl; cout<<"2: remove head from the list"<<endl; cout<<"3: see if an entered value is member of the list"<<endl; cout<<"4: quit the whole damned program"<<endl<<endl; cout<<"Enter your choice (Please)"<<endl; } wideglide[~/c_programs/c++ ]6%CC -g -fullwarn -o list_driver list_driver.cpp CC -g -fullwarn -o list_driver list_driver.cpp "listnode.h", line 9: error(3133): expected an identifier friend class List<DATA_TYPE>; "listnode.h", line 9: error(3358): invalid friend declaration friend class List<DATA_TYPE>; 2 errors detected in the compilation of "list_driver.cpp". wideglide[~/c_programs/c++ ]7%